﻿#include "stdafx.h"
#include "Atoi.h"

using namespace std;

namespace algorithm {
int CAtoi::From(std::string s)
{
    if (s.empty())
    {
        return 0;
    }
    int len = (int)s.length();
    int left = -1;
    //找到第一个不为空的字符
    for (int j = 0; j < len; j++)
    {
        if (s[j] == ' ')
        {
            continue;
        }
        else if (s[j] == '-' || s[j] == '+' || (s[j] >= '0' && s[j] <= '9'))
        {
            left = j;
            break;
        }
        else
        {
            break;
        }
    }
    if (left == -1)
    {
        return 0;
    }
    // 找到第一个不为0数字起始位置，如果有符号位，则将符号位赋值到最后一个前导0位置
    // 如-0000089 变为 ------89，其中left=5
    for (int j = left; j < len; j++)
    {
        if (s[j] == '-' || s[j] == '+')
        {
            for (int k = j; k < len; k++)
            {
                if (s[k] == '-' || s[k] == '+')
                {
                    left = k;
                    if (k + 1 < len)
                    {
                        if (s[k + 1] == '0')
                        {
                            s[k + 1] = s[k];
                            continue;
                        }
                    }
                }
                break;
            }
            break;
        }
        if (s[j] >= '0' && s[j] <= '9')
        {
            for (int k = j; k < len; k++)
            {
                if (s[k] >= '0' && s[k] <= '9')
                {
                    left = k;
                    if (s[k] == '0')
                    {
                        continue;
                    }
                }
                break;
            }
            break;
        }
    }
    //printf("left => %d\n", left);
    if (left == -1 || left >= len)
    {
        return 0;
    }
    int right = left;
    for (int j = left + 1; j < len; j++)
    {
        if (s[j] >= '0' && s[j] <= '9')
        {
            right = j;
            continue;
        }

        break;
    }
    //printf("right => %d\n", right);
    //printf("str => %s\n", s.substr(left, right-left+1).c_str());
    long long sum = 0;
    bool bNegative = false;
    bool bOverflow = false;
    int count = 1;
    for (; right >= left; right--)
    {
        if (s[right] == '-')
        {
            bNegative = true;
        }
        else if (s[right] == '+')
        {

        }
        else
        {
            if (!bOverflow)
            {
                // 没有溢出，则累加
                long long mul = (long)std::pow(10, count - 1);
                if (mul >= INT_MAX)
                {
                    // 次方后可能溢出
                    bOverflow = true;
                }
                long long v = (mul * (s[right] - '0'));
                if (v >= INT_MAX)
                {
                    // 乘积后可能溢出
                    bOverflow = true;
                }
                sum += v;
                count++;
            }
            if (sum < 0)
            {
                // 判断是否溢出了
                bOverflow = true;
            }
            //printf("sum => %d\n", sum);
        }
    }
    if (bOverflow)
    {
        return bNegative ? INT_MIN : INT_MAX;
    }
    else
    {
        return int(bNegative ? -sum : sum);
    }
}
}
